<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;

use app\admin\model\BanLog;
use app\admin\model\BoxGame;
use app\admin\model\Item;
use app\admin\model\Member;
use app\qsapi\model\BoxMemberInfo;

/**
 * 站点管理-服务类
 * @author ZhangXuanNeng
 * @since 2020/11/20
 * Class ItemService
 * @package app\admin\service
 */
class AccountService extends BaseService
{
    /**
     * 构造函数
     * @author ZhangXuanNeng
     * @since 2020/11/20
     * ItemService constructor.
     */
    public function __construct()
    {
        $this->model = new Member();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2020/11/20
     * @author ZhangXuanNeng
     */
    public function getList()
    {
        $param = request()->param();
        $map = [];
        if(isset($param['pid']) && !empty(['pid'])){
            $map[] = ['pid','=',$param['pid']];
        }else{
            $map[] = ['pid','=',0];
        }

        if(isset($param['id']) && !empty($param['id'])){
            $map[] = ['id', '=', $param['id']];
        }

        if(isset($param['nickname']) && !empty($param['nickname'])){
            $map[] = ['nickname', '=', $param['nickname']];
        }

        if(isset($param['phone']) && !empty($param['phone'])){
            $map[] = ['phone', '=', $param['phone']];
        }

        if (isset($param['time'][0]) && !empty($param['time'][0])) {
            $map[] = ['create_time', '>=', strtotime($param['time'][0])];

        }
        if (isset($param['time'][1]) && !empty($param['time'][1])) {
            $map[] = ['create_time', '<=', strtotime($param['time'][1])];
        }
        // 查询条件
        $data =  parent::getList($map);
        $game_ids=array_column($data['data'],'game_id');
        $game=(new BoxGame())->field('id,game_name')->whereIn('id',$game_ids)->select();
        $game_infos=array_column($game?$game->toArray():[],null,'id');

        $ban_ids=array_column($data['data'],'id');
        $banLog = (new BanLog())->whereIn('member_id',$ban_ids)->select();
        $ban_infos=array_column($banLog?$banLog->toArray():[],null,'member_id');

        foreach ($data['data'] as &$value){
            //查询用户是否被封禁
            $value['game']=isset($game_infos[$value['game_id']])?$game_infos[$value['game_id']]:[];
            $value['spend_amount'] = $value['spend_amount']??'0.00';
            $value['mid'] = $value['id'];
            $userBanInfo = isset($ban_infos[$value['id']])?$ban_infos[$value['id']]:[];
            if($userBanInfo){
                $value['ban_rule'] = $userBanInfo['ban_rule'];
                $value['ban_time'] = $userBanInfo['ban_time'] > time() ?(new MemberInfoService())->secondTime($userBanInfo['ban_time'] - time()):'';
            }
        }
        return $data;
    }

    /**
     * 添加或编辑
     * @return array|mixed
     * @since 2020/11/20
     * @author ZhangXuanNeng
     */
    public function edit()
    {
        // 请求参数
        $data = request()->param();

        // 图片处理
        $image = getter($data, 'image');
        if (!$data['id'] && !$image) {
            return message('请上传站点图片', false);
        }
        if (strpos($image, "temp")) {
            $data['image'] = save_image($image, 'item');
        } else {
            $data['image'] = str_replace(IMG_URL, "", $image);
        }
        return parent::edit($data); // TODO: Change the autogenerated stub
    }

    /**
     * 获取站点列表
     * @return array
     * @since 2020/11/20
     * @author ZhangXuanNeng
     */
    public function getItemList()
    {
        $list = $this->model->where("status", "=", 1)
            ->where("mark", "=", 1)
            ->order("sort", "asc")
            ->select()
            ->toArray();
        return message("操作成功", true, $list);
    }

}